<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="keywords" content="">
    <meta name="author" content="">
    <meta name="robots" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- PAGE TITLE HERE -->
    <title>职位申请</title>

    <link href="/css/nice-select.css" rel="stylesheet">
    <link href="/css/jquery.dataTables.min.css" rel="stylesheet">

    <!-- Style css -->
    <link href="/css/style.css" rel="stylesheet">

</head>
<body>
<div class="container-fluid">
    <div class="d-flex align-items-center mb-4 flex-wrap mt-4">
        <h4 class="fs-20 font-w600 me-auto">职位申请（招聘者管理）</h4>
        <div>
            <a href="new-job.html" class="btn btn-primary me-3 btn-sm"><i class="fas fa-plus me-2"></i>发布职位</a>
            <a href="javascript:void(0);" class="btn btn-secondary btn-sm me-3"> <i class="fas fa-envelope"></i></a>
            <a href="javascript:void(0);" class="btn btn-secondary btn-sm me-3"><i class="fas fa-phone-alt"></i></a>
            <a href="javascript:void(0);" class="btn btn-secondary btn-sm"><i class="fas fa-info"></i></a>

        </div>
    </div>
    <div class="row">
        <div class="col-xl-12">
            <div class="table-responsive">
                <table class="table display mb-4 dataTablesCard job-table table-responsive-xl card-table" id="example5">
                    <thead>
                    <tr>
                        <th>序号</th>
                        <th>职位标题</th>
                        <th>申请人姓名</th>
                        <th>申请人邮箱</th>
                        <th>联系电话</th>
                        <th>申请时间</th>
                        <th>发布者姓名</th>
                        <th>申请状态</th>
                        <th class="text-center">操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <!-- 动态渲染申请数据 -->
                    <tr th:each="apply : ${applies}">
                        <!-- 申请ID -->
                        <td th:text="${apply.id} ?: 'N/A'">1.</td>

                        <!-- 职位名称 -->
                        <td class="wspace-no" th:text="${apply.job?.title} ?: '未设置'">Database analyst</td>

                        <!-- 求职者姓名 -->
                        <td th:text="${apply.seeker?.info?.realName} ?: '匿名'">Jordan</td>

                        <!-- 求职者邮箱 -->
                        <td th:text="${apply.seeker?.info?.email} ?: '无邮箱'">Jordan@gmail.com</td>

                        <!-- 求职者电话 -->
                        <td th:text="${apply.seeker?.info?.phone} ?: '无电话'">1234598765</td>

                        <!-- 申请时间（格式化显示） -->
                        <!--                    <td th:text="${apply.applyTime ? #temporals.format(apply.applyTime, 'dd-MM-yyyy') : '无日期'}">24-01-2021</td>-->
                        <td th:text="${#temporals.format(apply.applyTime, 'yyyy-MM-dd')}"></td>
                        <!-- 职位发布者姓名 -->
                        <td th:text="${apply.job?.user?.info?.realName} ?: '未知发布者'">Nicholas</td>

                        <!-- 申请状态（动态样式） -->
                        <td>
        <span th:switch="${apply.status}">
            <!-- 待处理 -->
            <span th:case="0" class="btn btn-warning btn-sm btn-rounded">Pending</span>
            <!-- 已选择 -->
            <span th:case="1" class="btn btn-success btn-sm btn-rounded">Selected</span>
            <!-- 已拒绝 -->
            <span th:case="2" class="btn btn-danger btn-sm btn-rounded">Rejected</span>
            <!-- 空值处理 -->
            <span th:case="${null}" class="btn btn-secondary btn-sm btn-rounded">未知状态</span>
            <!-- 成功（默认） -->
            <span th:case="*" class="btn btn-success btn-sm btn-rounded">Success</span>
        </span>
                        </td>

                        <!-- 操作按钮 -->
                        <td class="action-btn wspace-no">
    <span>
        <a href="javascript:void(0);" class="approve-btn"  th:onclick="'approve(' + ${apply.id} + ')'">
            <i class="fas fa-check text-success"></i>
        </a>
    </span>
                            <span>
        <a href="javascript:void(0);" class="reject-btn" th:onclick="'reject(' + ${apply.id} + ')'">
            <i class="fas fa-times text-danger"></i>
        </a>
    </span>
                            <span>
        <a href="javascript:void(0);" class="view-btn" th:onclick="'view(' + ${apply.id} + ')'">
            <i class="fas fa-eye"></i>
        </a>
    </span>
                            <span>
        <a href="javascript:void(0);" class="delete-btn" th:onclick="'deleteApplication(' + ${apply.id} + ')'">
            <i class="far fa-trash-alt text-danger"></i>
        </a>
    </span>
                            <span>
        <a href="javascript:void(0);" class="download-btn"
           th:attr="data-hash=${apply.seeker?.resumeHash},
                   data-filename=${apply.seeker?.info?.realName ?: 'resume'}">
            <i class="fas fa-download text-warning"></i>
        </a>
    </span>
                        </td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<!-- 在 jQuery 之前引入 SweetAlert2 -->
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

<!-- Required vendors -->
<script src="/js/global.min.js"></script>
<script src="/js/Chart.bundle.min.js"></script>
<script src="/js/jquery.nice-select.min.js"></script>

<!-- Chart piety plugin files -->
<script src="/js/jquery.dataTables.min.js"></script>
<script src="/js/datatables.init.js"></script>
<script src="/js/custom.min.js"></script>
<script src="/js/dlabnav-init.js"></script>
<script src="/js/demo.js"></script>

<script>
    // 定义全局函数供th:onclick调用
    function approve(applyId) {
        updateApplicationStatus(applyId, 1);
    }

    function reject(applyId) {
        updateApplicationStatus(applyId, 2);
    }

    function view(applyId) {
        window.location.href = '/application/detail/' + applyId;
    }

    function deleteApplication(applyId) { // 注意：避免使用delete关键字
        if (confirm('确定要删除此申请记录吗？')) {
            $.ajax({
                url: '/api/application/delete/' + applyId,
                type: 'DELETE',
                success: function() {
                    alert('删除成功');
                    location.reload();
                },
                error: function(xhr) {
                    alert('删除失败: ' + (xhr.responseJSON?.message || '服务器错误'));
                }
            });
        }
    }

    // 优化后的状态更新函数
    function updateApplicationStatus(id, status) {
        $.ajax({
            url: '/api/application/get/' + id,
            type: 'GET',
            success: function(result) {
                const application = result.data;
                // 创建仅包含必要字段的更新对象
                const updateData = {
                    id: application.id,
                    status: status,
                    job: { id: application.job.id },
                    seeker: { id: application.seeker.id }
                };

                $.ajax({
                    url: '/api/application/update',
                    type: 'PUT',
                    contentType: 'application/json',
                    data: JSON.stringify(updateData),
                    success: function() {
                        Swal.fire({
                            icon: 'success',
                            title: '更新成功',
                            text: '状态更新成功',
                            showConfirmButton: false,
                            timer: 2000
                        }).then(() => {
                            location.reload();
                        });
                    },
                    error: function(xhr) {
                        // alert('更新失败: ' + (xhr.responseJSON?.message || '服务器错误'));
                        errorMsg = xhr.responseJSON?.message || '服务器错误';
                        Swal.fire({
                            icon: 'error',
                            title: '更新失败',
                            text: errorMsg
                        });
                    }
                });
            },
            error: function() {
                alert('获取申请数据失败');
            }
        });
    }

    // 下载简历处理（需要单独绑定）
    $(document).ready(function() {
        $(document).on('click', '.download-btn', function() {
            const resumeHash = $(this).data('hash');
            const filename = $(this).data('filename') || 'resume';

            if (!resumeHash) {
                alert('简历不存在');
                return;
            }

            window.open('/api/resume/download/' + resumeHash + '?filename=' + encodeURIComponent(filename), '_blank');
        });
    });
</script>


</body>
</html>